import java.util.Random;
import java.util.concurrent.*;

public class CyclicBarrierTest {
    public static void main(String[] args) {
        String[] heors = {"安其拉", "亚瑟", "马超", "张飞", "刘备"};
        ExecutorService service= Executors.newFixedThreadPool(5);
        CyclicBarrier barrier=new CyclicBarrier(5);
        for (int i=0;i<5;i++){
            service.execute(new Player(heors[i],barrier));
        }
        service.shutdown();
    }

    static class Player implements Runnable {
        private String hero;
        private CyclicBarrier barrier;

        public Player(String hero, CyclicBarrier barrier) {
            this.hero = hero;
            this.barrier = barrier;
        }

        @Override
        public void run() {
            try {
                TimeUnit.SECONDS.sleep(new Random().nextInt(10));
                System.out.println(hero+"加载进度100% 等带其他玩家加载完成中....");
                barrier.await();
                System.out.println(hero+":发现所有玩家加载完成，开始游戏");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

